<template>
  <div class="video_detail">
    <!-- 头部标题 -->
    <div class="head_tit">
      <div class="tit_fl">
        <img @click="$router.go(-1)" class="tit_fl_img" src="../assets/myImg/back@2x.png" alt />
      </div>
      <span>视频详情</span>
    </div>
    <!-- 视频播放 -->
    <div class="video">
      <div class="container">
        <div class="player">
          <video-player
            class="video-player"
            ref="videoPlayer"
            :playsinline="true"
            :options="playerOptions"
            @play="onPlayerPlay($event)"
            @pause="onPlayerPause($event)"
            @ended="onPlayerEnded($event)"
          ></video-player>
        </div>
      </div>
    </div>
    <!-- 视频简介 -->
    <div class="video_deta">
      <p>{{videoDetaList.chapter_name}}</p>
      <div>
        <span>￥{{videoDetaList.sale_price}}</span>
        <span>原价:{{videoDetaList.price}}</span>
      </div>
    </div>
    <!-- 下方课程简介 -->
    <div class="agreement">
      <h2 class="agreement_h2">课程简介</h2>
      <p class="agreement_info" v-html="videoDetaList.desc">{{videoDetaList.desc}}</p>
    </div>
    <!-- 底部收藏功能 -->
    <div class="video_harvest">
      <div class="harvest_fl" @click="harvest">
        <div v-if="status == 0">
          <img src="../assets/img/shou_n@2x.png" alt />
          <span>收藏</span>
        </div>
        <div v-if="status == 1">
          <img src="../assets/img/shou_s@2x.png" alt />
          <span>取消收藏</span>
        </div>
      </div>
      <div class="harvest_fr">
        <div
          class="fr_lqsp"
          @click="downloads(videoDetaList.chapter_url)"
          v-show="isDownVideo !== 0"
        >
          <img src="../assets/img/ling@2x.png" alt />
          <span>领取视频</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getVideoDetail,
  setCoursecollect,
  setUserCourseWatch,
} from "../api/videoDetail";
// 视频下载
import { ossDownloadFile } from "../utils/download";
// 节流函数
import { throtTling } from "../utils/throttling";
export default {
  name: "videoDetail",
  data() {
    return {
      // 动态显示收藏图标
      shouscImg: false,
      // 控制收藏和取消收藏字段
      status: "",
      type: "",
      videoId: "",
      // 定时器
      times: "",
      // 观看时间
      videoWatchTime: 0,
      // 是否可以下载视频
      isDownVideo: "",
      videoDetaList: "",
      playerOptions: {
        playbackRates: [0.7, 1.0, 1.5, 2.0], //播放速度
        autoplay: false, //如果true,浏览器准备好时开始回放。
        muted: false, // 默认情况下将会消除任何音频。
        loop: false, // 导致视频一结束就重新开始。
        preload: "auto", // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频（如果浏览器支持）
        language: "zh-CN",
        aspectRatio: "16:9", // 将播放器置于流畅模式，并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字（例如"16:9"或"4:3"）
        fluid: true, // 当true时，Video.js player将拥有流体大小。换句话说，它将按比例缩放以适应其容器。
        sources: [
          {
            src: "", // 路径
            type: "video/mp4", // 类型
          },
          {
            src: "//path/to/video.webm",
            type: "video/webm",
          },
        ],
        poster: "../../static/images/test.jpg", //你的封面地址
        notSupportedMessage: "此视频暂无法播放，请稍后再试", //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        controlBar: {
          timeDivider: true,
          durationDisplay: true,
          remainingTimeDisplay: false,
          fullscreenToggle: true, //全屏按钮
        },
      },
    };
  },
  methods: {
    // 获取数据
    getVideoDetail(id) {
      getVideoDetail(id).then((res) => {
        if (res.code == 200) {
          this.videoDetaList = res.data;
          console.log(this.videoDetaList);
          this.playerOptions.sources[0].src = res.data.chapter_url;
          this.playerOptions.poster = res.data.chapter_avatar;
          this.status = res.data.collect_status;
          this.isDownVideo = res.data.is_down;
        }
      });
    },
    // 收藏视频  节流函数优化
    harvest: throtTling(function () {
      let id = this.videoDetaList.chapter_id;
      if (this.status == 0) {
        // 未收藏
        this.type = 1;
      } else {
        // 已收藏
        this.type = 0;
      }
      setCoursecollect(id, this.type).then((res) => {
        if (res.code == 200) {
          this.shouscImg = true;
          this.status = this.type;
          this.$toast({
            duration: 2000,
            message: res.msg,
          });
        } else {
          this.$toast({
            duration: 2000,
            message: res.msg,
          });
        }
      });
    }),
    // 播放回调
    onPlayerPlay(e) {
      let that = this;
      // 观看时间记录
      window.clearInterval(that.times);
      that.times = setInterval(() => {
        that.videoWatchTime = that.videoWatchTime + 1;
      }, 1000);
    },
    //暂停回调
    onPlayerPause(e) {
      let that = this;
      window.clearInterval(that.times);
    },
    // 播放到结尾
    onPlayerEnded(e) {
      let that = this;
      // 观看时间记录
      window.clearInterval(that.times);
      that.times = setInterval(() => {
        that.videoWatchTime = that.videoWatchTime += 1;
      }, 1000);
      // 发送请求记录播放时间
      let data = {
        course_id: that.videoDetaList.course_id,
        chapter_id: that.videoDetaList.chapter_id,
        watched_duration: that.videoWatchTime,
        end_time: new Date().getTime(),
      };
      setUserCourseWatch(data).then((res) => {
        if (res.code == 200) {
          that.videoWatchTime = 0;
        }
      });
    },
    // 下载视频
    downloads: throtTling(function () {
      let url = this.videoDetaList.chapter_url;
      let arr = url.split("video");
      let videoUrl = "video" + arr[1];
      ossDownloadFile(videoUrl, this.videoDetaList.chapter_name);
    }, 1000),
    // downloads() {
    //   let url = this.videoDetaList.chapter_url;
    //   let arr = url.split("video");
    //   let videoUrl = "video" + arr[1];
    //   // 视频下载
    //   ossDownloadFile(videoUrl, this.videoDetaList.chapter_name);
    // },
  },
  mounted() {
    this.videoId = this.$route.query.id;
    this.getVideoDetail(this.videoId);
  },
  computed: {
    player() {
      return this.$refs.videoPlayer.player;
    },
  },
  destroyed() {
    let that = this
    // 发送请求记录播放时间
    let data = {
      course_id: that.videoDetaList.course_id,
      chapter_id: that.videoDetaList.chapter_id,
      watched_duration: that.videoWatchTime,
      end_time: new Date().getTime(),
    };
    setUserCourseWatch(data).then((res) => {
      if (res.code == 200) {
        that.videoWatchTime = 0;
      }
    });
  },
};
</script>

<style lang="less" scoped>
.video_detail {
  background: #fafafa;
}
.video {
  height: 5.6rem;
  background: pink;
}
.video_deta {
  padding: 0.533333rem;
  height: 2.133333rem;
  font-size: 0.453333rem;
  color: #333;
  line-height: 0.586667rem;
  background: #fff;
  div {
    text-align: right;
    & span:nth-child(1) {
      margin-right: 0.133333rem;
      color: #ff4400;
    }
    & span:nth-child(2) {
      color: #999;
      font-size: 0.32rem;
    }
  }
}

.video_harvest {
  display: flex;
  justify-content: space-between;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 1.453333rem;
  background: #fff;
  box-shadow: 0px 3px 7px 0px rgba(0, 0, 0, 0.35);

  div {
    flex: 1;
  }
  .harvest_fl {
    font-size: 0.4rem;
    color: #999;
    line-height: 1.466667rem;
    text-align: center;
    img {
      margin-right: 0.266667rem;
      width: 0.64rem;
      height: 0.64rem;
    }
  }
  .harvest_fr {
    .fr_lqsp {
      margin: auto;
      margin-top: 0.32rem;
      width: 3.2rem;
      height: 0.88rem;
      line-height: 0.88rem;
      text-align: center;
      color: #fff;
      background: #647cfa;
      border-radius: 0.44rem;
      img {
        margin-right: 0.266667rem;
        width: 0.466667rem;
        height: 0.466667rem;
      }
    }
  }
}
.head_tit {
  position: relative;
  text-align: center;
  color: #333;
  font-weight: 700;
  height: 1.333333rem;
  line-height: 1.333333rem;
  font-size: 0.453333rem;
  .tit_fl {
    position: absolute;
    left: 0;
    top: 0.266667rem;
    width: 0.706667rem;
    height: 0.706667rem;
    border-radius: 50%;
    .tit_fl_img {
      position: relative;
      top: -0.32rem;
      width: 0.386667rem;
      height: 0.386667rem;
    }
  }
}
.agreement {
  margin-top: 0.133333rem;
  padding: 0.4rem;
  height: 8rem;
  background: #fff;
  color: #999;
  font-size: 0.4rem;
  .agreement_h2 {
    margin-bottom: 0.533333rem;
    font-size: 0.453333rem;
    color: #333;
  }
  .agreement_info {
    line-height: 0.586667rem;
  }
}
</style>